<?php

namespace App\Admin\Controllers;

use App\Admin\Extensions\Tools\ReleasePost;
use App\VideoType;
use App\Tools\AliOss;
use App\VideoList;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;
use Illuminate\Support\Facades\DB;
use App\Http\Controllers\GetVideoUrl;
use Encore\Admin\Layout\Content;
use Encore\Admin\Widgets\InfoBox;

class VideoListController extends AdminController
{
    /**
     * Title for current resource.
     *
     * @var string
     */
//    protected $title = '视频管理';
    public function index(Content $content)
    {
        return $content
            ->header('视频管理')
            ->description('')
            ->row(function ($row){
                $num = VideoList::count();
                $release_num = VideoList::where('show_state',1)->count();
                $view_count = VideoList::sum('view_num') - 1000*$num;
                $today_num = VideoList::whereDate('created_at',date('Y-m-d'))->count();


                $row->column( 3, new InfoBox('总视频量', 'book', 'blue', '', $num));
                $row->column( 3, new InfoBox('已发布视频量', 'book', 'red', '', $release_num));
                $row->column( 3, new InfoBox('总浏览量', 'book', 'orange', '', $view_count));
                $row->column( 3, new InfoBox('今日新增视频数', 'book', 'green', '', $today_num));

            })
            ->body($this->grid());
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new VideoList);
        $grid->filter(function($filter){
            // 去掉默认的id过滤器
            $filter->disableIdFilter();
            // 在这里添加字段过滤器
            $filter->like('title', '视频标题');
        });
        $grid->model()->where('show_state',1)->orderBy('id','desc');
        $grid->column('id', '视频编号')->sortable();
        $grid->column('title', '视频标题')->display(function ($title){
            if(mb_strlen($title,'UTF8') > 15){
                return '<p title="'.$title.'">'.mb_substr($title,0,12,'utf-8').' ...</p>';
            }else{
                return '<p title="'.$title.'">'.mb_substr($title,0,12,'utf-8').'</p>';
            }
        });
        $grid->column('img_url','封面图')->image('', 80, 80);
        $grid->column('share_img','分享图')->image('', 80, 80);
        $grid->column('video_time','视频时长')->sortable();
        $grid->column('view_num', '观看次数')->sortable();
        $state = [
            'on'  => ['value' => 1, 'text' => '打开', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '关闭', 'color' => 'danger'],
        ];
        $grid->show_state('是否展示')->switch($state)->sortable();
        $grid->advert_state('前贴广告')->switch($state)->sortable();
        $grid->column('release_at', '发布时间')->sortable();
        $grid->column('created_at', '创建时间')->sortable();
        $grid->actions(function ($actions) {
            // 去掉查看
            $actions->disableView();
        });

        $grid->tools(function (Grid\Tools $tools){
            $tools->batch(function ($batch){
                $batch->add('批量取消发布', new ReleasePost(0));
            });
        });

        return $grid;
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     * @return Show
     */
    protected function detail($id)
    {

    }
    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new VideoList);
        $form->display('id');
        $form->text('title', '视频标题')->required();
        $form->image('img_url', '封面图')->uniqueName()->removable();
        $form->image('share_img', '分享图')->uniqueName()->removable();
        $form->text('video_desc', '视频简介');
        $form->text('video_time', '视频时长')->default("5:00");
        $form->number('view_num', '观看次数')->default(1000);
        $form->text('video_url', '视频链接地址');
        $form->switch('show_state', '是否展示')->default(0);
        $form->switch('advert_state', '前贴广告')->default(0);
        $form->text('release_at', '发布时间')->default(date('Y-m-d H:i:s'));
        $form->display('created_at', '创建时间');
        $form->display('updated_at', '更新时间');
        $form->saved(function (Form $form){
            $data = [];
            if(!$form->model()->video_url && $form->model()->vid){
                $getUrl = new GetVideoUrl();
                $video_url = $getUrl->get_mp4($form->model()->vid);
                if($video_url){
                    $data['video_url'] = $video_url;
                }
            }
            $img_url = $form->model()->img_url;
            $share_img = $form->model()->share_img;
            //dd($img_url,$share_img);
            $oss = new AliOss();
            $oss_domain = isset($oss->config['OSS_VIEW_HOST']) ? $oss->config['OSS_VIEW_HOST'] : 'http://lzy-hangzhou.liziyun.net.cn/';
            //封面图
            if(!strstr($img_url, 'http')){
                $img_url_path = $_SERVER['DOCUMENT_ROOT'].'/'.$img_url;
                $oss_img_url = 'ytsp/'.date('Ymd').'/'.$img_url;
                if($oss->toOSS($oss_img_url,$img_url_path)){
                    $data['img_url'] = $oss_domain.$oss_img_url;
                }else{
                    $data['img_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/'.$img_url;
                }
            }
            //分享图
            if(!strstr($share_img, 'http')){
                $share_img_path = $_SERVER['DOCUMENT_ROOT'].'/'.$share_img;
                $oss_share_img = 'ytsp/'.date('Ymd').'/'.$share_img;
                if($oss->toOSS($oss_share_img,$share_img_path)){
                    if(env('WATE_MARK')){
                        //图片加水印
                        $watemark_img = 'ytsp/images/play.png';
                        $data['share_img'] = $oss->watermark($oss_share_img,$watemark_img,60,80,'center',-15);
                    }else{
                        $data['share_img'] = $oss_domain.$oss_share_img;
                    }
                }else{
                    $data['share_img'] = 'http://'.$_SERVER['SERVER_NAME'].'/'.$share_img;
                }
            }
            if($data){
                $data['updated_at'] = date('Y-m-d H:i:s');
                $form->model()->where('id',$form->model()->id)->update($data);
            }

        });
        return $form;
    }

    public function unrelease()
    {
        $ids = explode(',', $_POST['ids']);

        foreach ($ids as $id) {
            DB::table('zm_video_list')->where('id', $id)->update(['show_state' => '0']);
        }
        return array(['state' => 200, 'msg' => '发布成功']);
    }

}
